"use strict";
const mvc = require('cosjs.mvc');
const random = mvc.library.require('random');
const time = mvc.library.require('time');
/**
 * @name monthSign
 * 月签到
 */


/**
 * @name monthGift
 * @param int id 月签到表里的id
 * 月签到
 */
exports.monthGift = async function () {
    let id = this.get('id', 'int') || 0;
    let nowYY = time.sign(0, "yyyy");
    let nowMM = time.sign(0, "MM");
    let nowDD = time.sign(0, "dd");
    // let now = time.today();
    if (id == 0) {
        id = nowDD;
    }
    if (id != nowDD) {
        return this.quick_error('M2AGHRPF101', '日期错误', id);
    }
    let flag = 0;
    let luckFlag = -1;
    let nowMonth = nowMM;
    let config = this.config('monthGift', nowMonth);
    if (!config) {
        return this.quick_error('M2AGHRPF101', '配置不存在', id);
    }
    //获取已经签到的天数
    this.updater.daily.key('mSigns');
    this.updater.role.key('mSigns');
    await this.updater.data()
    // let dSigns = this.updater.daily.get('mSigns') || 0;
    // if (dSigns) {
    //     return this.quick_error('M2AGHRPF1013', '今天已经签到过了');
    // }
    let signs = this.updater.role.get('mSigns') || { 'year': 0, 'month': 0, 'days': [], 'total': [] };
    if ((signs.month != nowMonth) || (signs.year != nowYY)) {
        //重置
        signs.year = nowYY;
        signs.month = nowMonth;
        signs.remedy = 0;
        signs.days = [];
        signs.total = [];
    }
    // if (id < nowDD) {
    //     //补签
    //     flag = 1;
    //     signs.remedy++;
    //     let costConfig = this.config('remedy', signs.remedy);
    //     if (!costConfig) {
    //         return this.quick_error('M2AGHRPF101', '配置不存在', signs.remedy);
    //     }
    //     this.updater.item.sub(costConfig.item, costConfig.amount);
    // }
    for (let i = 0; i < signs.days.length; i++) {
        if (id == signs.days[i]) {
            return this.quick_error('M2AGHRPF1013', '这天已经签到过了');
        }
    }
    signs.days.push(id);
    //日奖励
    for (let i in config.Coll) {
        if (config.Coll[i].day == id) {
            this.updater.add(config.Coll[i].gift, config.Coll[i].amount);
            //判断今日运势
            if (!flag) {
                let rateGiftConfig = this.config('rateGift', config.Coll[i].specialDay);
                if (!rateGiftConfig) {
                    return this.quick_error('M2AGHRPF101', '配置不存在', id);
                }
                let k = random.Relative(rateGiftConfig.Coll, 'rate');
                if (k === false) {
                    return this.quick_error("M7GHLB123", 'err request', k);
                }
                if (rateGiftConfig.Coll[k].gift != 0) {
                    this.updater.add(rateGiftConfig.Coll[k].gift, 1);
                }
                //TODO:luckey
                luckFlag = rateGiftConfig.Coll[k].sort;
            }
        }
    }

    //累计奖励
    let totalConfig = this.config('timeGift', nowMonth);
    if (!totalConfig) {
        return this.quick_error('M2AGHRPF101', '配置不存在', nowMonth);
    }
    for (let i in totalConfig.Coll) {
        if (totalConfig.Coll[i].times == signs.days.length) {
            signs.total.push(totalConfig.Coll[i].times);
            this.updater.add(totalConfig.Coll[i].gift, totalConfig.Coll[i].amount);
        }
    }
    // rateGift
    this.updater.role.set("mSigns", signs);
    this.updater.daily.set('mSigns', 1);
    await this.updater.save();
    return luckFlag;
}

/**
 * @name monthCardGift
 * @param string id 用户唯一ID
 * 月卡签到奖励
 */
exports.monthCardGift = async function () {
    let id = this.get('id', 'string');
    if (!id) {
        return this.quick_error("M2saghge10", "参数缺失key");
    }

    //检查月卡是否到期
    this.updater.role.key('monthCardExpire');
    this.updater.daily.key('monthCardAwardDailyCheck');
    await this.updater.data();
    let monthCardExpire = this.updater.role.get('monthCardExpire');
    if (monthCardExpire < Date.now()) {
        return this.quick_error('月卡到期，不能参与该签到活动！');
    }
    
    let montheyAwardgetConfig = this.config('montheyAwardget', 60100998);
    if (!montheyAwardgetConfig) {
        return this.quick_error('config_not_exist');
    }
    
    let mcAwardDailyCheck = this.updater.daily.get('monthCardAwardDailyCheck');
    if (mcAwardDailyCheck) {
        return this.quick_error('今日已签到，明日再来～');
    }
    mcAwardDailyCheck = 1;
    this.updater.item.add(parseInt(montheyAwardgetConfig.monthlyDailyaward), montheyAwardgetConfig.monthlyDailyaward_number);
    this.updater.daily.set('monthCardAwardDailyCheck',mcAwardDailyCheck);
    return this.updater.save();
}